
package com.starshineworks.util;

import java.util.ArrayList;

public class Menu {

    private MenuManager         mgr;
    private String              varName;
    private Integer             id;
    private ArrayList<String[]> options;
    private Integer             numSel;
    private String              type;

    public Menu(MenuManager mgr, String varName, Integer numSel, String type, Integer id) {
        this.id         = id;
        this.varName    = varName;
        this.options    = new ArrayList<String[]>();
        this.numSel     = numSel;
        this.type       = type;
    }
    
    public String getVarName() {
        return this.varName;
    }

    public String getOptionIndex(Integer i) {
        return options.get(i)[0];
    }

    public String getOptionName(Integer i) {
        return options.get(i)[2];
    }

    public String getOptionValue(Integer i) {
        return options.get(i)[1];
    }
    
    public void addOption(Integer where, String[] e) {
        while (options.size() < (where + 1)) {
            String[] elm = {"0", "0", "----"};
            options.add(elm);
        }
        options.set(where, e);
    }
    
    public Integer getSize() {
        return options.size();
    }
    
    String toHTML(Integer rowId) {
        String y = "";
        
        if (type.equalsIgnoreCase("drop")) {
            String mult = "";
            Integer ini = 0;
            if (numSel > 1) {
                mult = " size="+numSel+"  ";// XXX multiple=multiple size="+numSel;
                ini = 1;
            } 
            y += "<select class=ssw_event menuId="+id+" name="+getVarName()+rowId+"[] "+mult+" rowId="+rowId+" >";
            for (Integer o = ini; o < options.size(); o++) {
                y += "<option textval='"+getOptionName(o)+ "' " +
                             "  value='"+getOptionName(o)+ "' " +
                             "  sswindex='"+o+"' >"+getOptionName(o)+"</option>";
            }
            y += "</select>";
        }
        
        if (type.equalsIgnoreCase("check")) {
            for (Integer o = 1; o < options.size(); o++) {
                y += "<input textval='"+getOptionName(o)+"' " + 
                             " value='"+getOptionName(o)+"' " +
                             " sswindex='"+o+"' " +
                             " class=ssw_event rowId="+rowId+" menuId="+id+" type=checkbox name="+getVarName()+rowId+"[] >"+getOptionName(o)+"</input><br/>";
            }
        }
        
        if (type.equalsIgnoreCase("radio")) {
            for (Integer o = 1; o < options.size(); o++) {
                y += "<input textval='"+getOptionName(o)+"' " + 
                        " value="+getOptionName(o)+" "+
                        " sswindex="+o+" "+
                        " class=ssw_event rowId="+rowId+"  menuId="+id+" type=radio "+
                        " name="+getVarName()+rowId+"[] >"+getOptionName(o)+"</input><br/>";
            }
        }
        
        return y;
    }

}
